<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handlePrint()" v-auth="''">
          <Icon icon="ant-design:printer-outlined" /> {{ t('箱码打印') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ modid: record.modid })">
          {{ record.mocode }}
        </a>
      </template>
    </BasicTable>
    <!-- <InputForm @register="registerDrawer" @success="handleSuccess" /> -->
    <BasicModal
      v-bind="$attrs"
      @register="registerModal"
      title="生产订单箱码打印"
      @ok="handleModal"
      :width="1000"
    >
      <BasicTable @register="registerPuArrTable" @row-click="handlePuArrRowClick" />
    </BasicModal>
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoOrderWmsMoOrderList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    wmsMoOrderDelete,
    wmsMoOrderListData,
    wmsMoOrderListDataByStaus,
  } from '/@/api/wms/mo/order/wmsMoOrder';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import PrintModal from '/@/components/print/printModal.vue';
  import { formatToDate } from '/@/utils/dateUtil';

  const { t } = useI18n('mo.order.wmsMoOrder');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('生产订单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('modid'),
        field: 'modid',
        component: 'Input',
        show: false,
      },
      {
        label: t('moid'),
        field: 'moid',
        component: 'Input',
        show: false,
      },
      {
        label: t('订单编号'),
        field: 'mocode',
        component: 'Input',
      },
      {
        label: t('行号'),
        field: 'sortseq',
        component: 'Input',
        show: false,
      },
      {
        label: t('存货编码'),
        field: 'invcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('部门编码'),
        field: 'mdeptcode',
        component: 'Input',
      },
      {
        label: t('部门名称'),
        field: 'mdeptname',
        fieldLabel: 'mdeptname',
        component: 'TreeSelect',
        componentProps: {
          dictType: 'dept',
          canSelectParent: false,
          allowClear: true,
        },
      },
      {
        label: t('数量'),
        field: 'qty',
        component: 'Input',
        show: false,
      },
      {
        label: t('状态'),
        field: 'mostatus',
        component: 'Select',
        componentProps: {
          allowClear: true,
          dictType: 'U8_MO_ORDER_STATUS',
        },
      },
      {
        label: t('领料状态'),
        field: 'llstatus',
        component: 'Select',
        componentProps: {
          allowClear: true,
          dictType: 'U8_MO_ORDER_LL_STATUS',
        },
      },
      {
        label: t('单据日期'),
        field: 'ddate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('开始日期'),
        field: 'startdate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('结束日期'),
        field: 'duedate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('modid'),
      dataIndex: 'modid',
      key: 'a.modid',
      sorter: true,
      width: 230,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('moid'),
      dataIndex: 'moid',
      key: 'a.moid',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('领料状态'),
      dataIndex: 'llstatus',
      key: 'a.llstatus',
      sorter: true,
      width: 130,
      dictType: 'U8_MO_ORDER_LL_STATUS',
      align: 'center',
    },
    {
      title: t('订单编号'),
      dataIndex: 'mocode',
      key: 'a.mocode',
      sorter: true,
      width: 130,
      align: 'left',
      //slot: 'firstColumn',
    },
    {
      title: t('行号'),
      dataIndex: 'sortseq',
      key: 'a.sortseq',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('单据日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invcode',
      key: 'a.invcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'a.invcode',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'basInv.cinvstd',
      key: 'inv.cinvstd',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('单位'),
      dataIndex: 'basInv.ccomunitname',
      key: 'inv.ccomunitname',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('启用批次'),
      dataIndex: 'basInv.binvbatch',
      key: 'inv.binvbatch',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('部门编码'),
      dataIndex: 'mdeptcode',
      key: 'a.mdeptcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('部门名称'),
      dataIndex: 'mdeptname',
      key: 'a.mdeptname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'qty',
      key: 'a.qty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('状态'),
      dataIndex: 'mostatus',
      key: 'a.mostatus',
      sorter: true,
      width: 130,
      dictType: 'U8_MO_ORDER_STATUS',
      align: 'center',
    },
    {
      title: t('开始日期'),
      dataIndex: 'startdate',
      key: 'a.startdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('结束日期'),
      dataIndex: 'duedate',
      key: 'a.duedate',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const tableColumnsPrint: BasicColumn[] = [
    {
      title: t('存货编码'),
      dataIndex: 'invcode',
      key: 'a.invcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'a.cinvname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'basInv.cinvstd',
      key: 'a.cinvstd',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'qty',
      key: 'a.qty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('打印张数'),
      dataIndex: 'counts',
      key: 'a.counts',
      sorter: true,
      width: 130,
      align: 'center',
      editable: true,
      edit: true,
      editRow: true,
      editComponent: 'InputNumber',
      // editDefaultValue: '1',
      editComponentProps: {
        maxlength: 16,
      },
      editRule: true,
    },
  ];

  const [registerPuArrTable, boxCodePrintTable] = useTable({
    columns: tableColumnsPrint,
    rowKey: 'id',
    pagination: false,
    bordered: true,
  });

  function handlePuArrRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  async function getBoxCodeList() {
    let boxCodeValid = true;
    let boxCodeList: Recordable[] = [];
    for (const record of boxCodePrintTable.getDataSource()) {
      if (!(await record.onEdit?.(false, true))) {
        boxCodeValid = false;
      }
      boxCodeList.push({
        ...record,
        id: !!record.isNewRecord ? '' : record.id,
      });
    }
    for (const record of boxCodePrintTable.getDelDataSource()) {
      if (!!record.isNewRecord) continue;
      boxCodeList.push({
        ...record,
        status: '1',
      });
    }
    if (!boxCodeValid) {
      throw { errorFields: [{ name: ['boxCodeList'] }] };
    }
    return boxCodeList;
  }

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑生产订单'),
        onClick: handleForm.bind(this, { modid: record.modid }),
        auth: 'mo:order:wmsMoOrder:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除生产订单'),
        popConfirm: {
          title: t('是否确认删除生产订单'),
          confirm: handleDelete.bind(this, { modid: record.modid }),
        },
        auth: 'mo:order:wmsMoOrder:edit',
      },
    ],
  };

  onMounted(async () => {
    //handleDefautlSearch();
  });

  const [registerPrintModal, { openModal }] = useModal();
  const [registerModal, { openModal: openPrintModal, closeModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: wmsMoOrderListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await wmsMoOrderDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handlePrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    await openPrintModal(true, {});
    arr.forEach((element) => {
      let count = Math.ceil(Number(element.qty) / Number(element.basInv.boxdefnum))
        ? Math.ceil(Number(element.qty) / Number(element.basInv.boxdefnum))
        : 1;
      element.counts = count;
    });
    boxCodePrintTable.setTableData(arr);
  }

  async function handleModal() {
    const data = await getBoxCodeList();
    let params = {
      ids: '',
      counts: '',
      title: '生产订单箱码打印',
      height: '0.9',
      width: '0.9',
      fileName: '生产订单箱码打印',
    };
    openModal(true, params);
    data.forEach((e) => {
      params.ids += e.modid + ',';
      params.counts += e.counts + ',';
    });
    params.ids = params.ids.substring(0, params.ids.length - 1);
    params.counts = params.counts.substring(0, params.counts.length - 1);
    openPrintModal(true, params);
    closeModal();
    boxCodePrintTable.setTableData([]);
  }

  async function handleDefautlSearch() {
    await getForm().setFieldsValue({
      startdate: formatToDate(new Date()),
    });
    reload();
    await getForm().setFieldsValue({
      startdate: formatToDate(new Date()),
    });
  }

  function handleSuccess() {
    reload();
  }
</script>
<style scoped>
  :deep(.jeesite-editable-cell__action) {
    display: none;
  }

  /* .jeesite-editable-cell__action {
    display: none;
  } */

  :deep(.edit-cell-align-center) {
    width: 100% !important;
  }
</style>
